package com.example.orderassembly.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.api.GoodApi;
import com.woniu.cloud.api.StoreGoodApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TGood;
import com.woniu.cloud.entity.TStoreGood;
import com.woniuxy.entity.StorePurchaseDetailOrder;
import com.example.orderassembly.mapper.StorePurchaseDetailOrderMapper;
import com.example.orderassembly.service.IStorePurchaseDetailOrderService;
import com.woniuxy.form.InsertStorePurchaseDetailOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 店铺进货订单详情表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
public class StorePurchaseDetailOrderServiceImpl extends ServiceImpl<StorePurchaseDetailOrderMapper, StorePurchaseDetailOrder> implements IStorePurchaseDetailOrderService {
    @Resource
    StorePurchaseDetailOrderMapper storePurchaseDetailOrderMapper;
    @Resource
    GoodApi goodApi;

    @Override
    public Boolean insertStorePurchaseDetailOrder(InsertStorePurchaseDetailOrder insertStorePurchaseDetailOrder) {
        // todo  通过jwt 获得店铺id
        Integer storeId  = insertStorePurchaseDetailOrder.getUserId();
        StorePurchaseDetailOrder storePurchaseDetailOrder = new StorePurchaseDetailOrder();
        //设置店铺id
        storePurchaseDetailOrder.setStorePurchaseDetailOrderStoreId(storeId);
        //设置进货订单Id
        storePurchaseDetailOrder.setStorePurchaseDetailOrderStorePurchaseOrderId(insertStorePurchaseDetailOrder.getStorePurchaseOrder());
        HashMap<Integer, Integer> map = insertStorePurchaseDetailOrder.getServiceAttribute();
        Set<Integer> set = map.keySet();
        Integer num = 0;
        for (Integer key : set) {
            Result storeGoodByStoreGoodId = goodApi.getGoodByGoodId(key);
            TGood data  = JSON.parseObject(JSON.toJSONString(storeGoodByStoreGoodId.getData()), TGood.class);
            storePurchaseDetailOrder.setStorePurchaseDetailOrderNum(map.get(key));
            storePurchaseDetailOrder.setStorePurchaseDetailOrderGoodId(key);
            storePurchaseDetailOrder.setStorePurchaseDetailOrderGoodName(data.getGoodName());
            storePurchaseDetailOrder.setStorePurchaseDetailOrderGoodPrice(new BigDecimal(data.getGoodPrice()));
            storePurchaseDetailOrder.setStorePurchaseDetailTotal(new BigDecimal(map.get(key)*data.getGoodPrice()));
            num += storePurchaseDetailOrderMapper.insert(storePurchaseDetailOrder);
        }
        return num==set.size();
    }

    @Override
    public List<StorePurchaseDetailOrder> getStorePurchaseDetailOrderByStorePurchaseOrderId(Integer orderId) {
        QueryWrapper<StorePurchaseDetailOrder> storePurchaseDetailOrderQueryWrapper = new QueryWrapper<>();
        storePurchaseDetailOrderQueryWrapper.eq("store_purchase_detail_order_store_purchase_order_id", orderId);
        List<StorePurchaseDetailOrder> list = storePurchaseDetailOrderMapper.selectList(storePurchaseDetailOrderQueryWrapper);
        return list;
    }
}
